<template>
  <el-menu
      :default-active="this.$route.name"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      :collapse="isCollapse"
      background-color="rgb(39, 42, 55)"
      text-color="#fff"
      active-text-color="#ffd04b"
  >
<!--    <h3> {{ isCollapse ? '▽' : 'TO BE BETTER' }}</h3>-->
    <el-menu-item @click="clickMenu(item)" v-for="item in noChildren" :key="item.name" :index="item.name">
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu v-for="item in hasChildren" :key="item.label" :index="item.label">
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title"> {{ item.label }}</span>
      </template>
      <el-menu-item-group v-for="subItem in item.children" :key="subItem.path">
        <el-menu-item @click="clickMenu(subItem)" :index="subItem.path"><span style="margin-left: 30px">{{
            subItem.label
          }}</span></el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu {
  height: 100vh;

  h3 {
    color: #fff;
    text-align: center;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400;
  }
}
</style>

<script>
export default {
  data() {
    return {
      //isCollapse: false,  //下面computed定义了，这里就去掉
      menuData: [
        {
          path: "/",
          name: "home",
          label: "首页",
          icon: "s-home",
          url: "Home/Home"
        },
        {
          path: "/user",
          name: "user",
          label: "用户管理",
          icon: "user",
          url: "UserManage/UserManage"
        },
        {
          label: "帖子管理",
          icon: "location",
          children: [
            {
              path: "/post/query",
              name: "postQuery",
              label: "帖子查询",
              icon: "setting",
              url: "/post/query"
            }
          ]
        },
        {
          label: "报表管理",
          icon: "location",
          children: [
            {
              path: "/report/export",
              name: "reportExport",
              label: "导出列表",
              icon: "setting",
              url: "/report/export"
            }
          ]
        },
        {
          path: "/message/chatRoom",
          name: "messageChatRoom",
          label: "聊天室",
          icon: "user",
          url: "/message/chatRoom"
        },
        {
          label: "AI",
          icon: "location",
          children: [
            {
              path: "/ai/deepseek",
              name: "deepseek",
              label: "deepseek",
              icon: "setting",
              url: "/ai/deepseek"
            }
          ]
        },
        {
          label: "审批管理",
          icon: "location",
          children: [
            {
              path: "/approve/approveList",
              name: "approveList",
              label: "审批列表",
              icon: "setting",
              url: "/approve/approveList"
            }
          ]
        },
        {
          label: "产品管理",
          icon: "location",
          children: [
            {
              path: "/product/productList",
              name: "productList",
              label: "产品列表",
              icon: "setting",
              url: "/product/productList"
            }
          ]
        },
        {
          label: "积分管理",
          icon: "location",
          children: [
            {
              path: "/points/pointsRank",
              name: "pointsRank",
              label: "积分排行",
              icon: "setting",
              url: "/approve/pointsRank"
            },
            {
              path: "/points/pointsDetail",
              name: "pointsDetail",
              label: "积分明细",
              icon: "setting",
              url: "/approve/pointsDetail"
            },
            {
              path: "/points/pointsChannelList",
              name: "pointsChannelList",
              label: "渠道管理",
              icon: "setting",
              url: "/approve/pointsChannelList"
            }
          ]
        },
        {
          label: "消息中心",
          icon: "location",
          children: [
            {
              path: "/message/list",
              name: "messageList",
              label: "消息列表",
              icon: "setting",
              url: "/message/list"
            }
          ]
        },
        {
          label: "权限管理",
          icon: "location",
          children: [
            {
              path: "/roleMgr/role",
              name: "roleMgrRole",
              label: "角色管理",
              icon: "setting",
              url: "roleMgr/role"
            },
            {
              path: "/roleMgr/user",
              name: "roleMgrUser",
              label: "用户管理",
              icon: "setting",
              url: "roleMgr/user"
            },
            {
              path: "/roleMgr/group",
              name: "roleMgrGroup",
              label: "权限组管理",
              icon: "setting",
              url: "roleMgr/group"
            },
            {
              path: "/roleMgr/resource",
              name: "roleMgrResource",
              label: "接口权限管理",
              icon: "setting",
              url: "roleMgr/resource"
            },
          ]
        },
        {
          label: "测试页面",
          icon: "location",
          children: [
            {
              path: "/test/testView",
              name: "testView",
              label: "测试页面1",
              icon: "setting",
              url: "/test/testView"
            }
          ]
        },
      ],
    };
  },
  methods: {
    //这里key, keyPath用不上，直接去掉了
    handleOpen() {
      //展开菜单
    },
    handleClose() {
      //关闭菜单
    },
    // 点击菜单
    clickMenu(item) {
      //路由跳转，当页面的路由与跳转的路由不一致才允许跳转
      if ((this.$route.path !== item.path) &&
          !(this.$route.path === '/home' && (item.path === '/'))) {
        this.$router.push(item.path)
      }

      this.$store.commit('tab/selectMenu', item)
    }
  },
  computed: {
    //没有子菜单
    noChildren() {
      return this.menuData.filter(item => !item.children)
    },
    //有子菜单
    hasChildren() {
      return this.menuData.filter(item => item.children)
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse
    }
  }
}
</script>

<style>
.el-menu {
  border-right: none;
}

.el-menu-item-group__title {
  padding: 0 !important;
}
</style>